package com.gy.zpchat.net;

import android.os.Handler;
import android.os.Looper;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.gy.zpchat.base.BaseCallBack;
import com.gy.zpchat.util.AppLogger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Response;

/**
 * OkHttpMange管理类
 * Created by leict on 2017/6/6.
 */

public class OkHttpManager {
    private static OkHttpManager mInstance;
    private OkHttpClient mClient;
    private Handler mHnadler;
    private Gson mGson;

    /**
     * 单例
     *
     * @return
     */
    public static synchronized OkHttpManager getInstance() {
        if (mInstance == null) {
            mInstance = new OkHttpManager();
        }
        return mInstance;
    }

    /**
     * 构造函数
     */
    private OkHttpManager() {
        initOkHttp();
        mHnadler = new Handler(Looper.getMainLooper());
        mGson = new Gson();
    }

    /**
     * 初始化OkHttpClient
     */
    private void initOkHttp() {
        mClient = new OkHttpClient().newBuilder()
                .readTimeout(30000, TimeUnit.SECONDS)
                .connectTimeout(30000, TimeUnit.SECONDS)
                .writeTimeout(30000, TimeUnit.SECONDS)
                .build();
    }

    /**
     * 请求
     *
     * @param client
     * @param callBack
     */
    public void request(BaseOkHttpClient client, final BaseCallBack callBack) {
        if (callBack == null) {
            throw new NullPointerException(" callback is null");
        }
        mClient.newCall(client.buildRequest()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                sendonFailureMessage(callBack, e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    AppLogger.getInstance().d("返回结果:%s", result);
                    JSONObject resultJson=JSONObject.parseObject(result);
                    if(resultJson.getString("code").equals("00000")){
                        sendonSuccessMessage(callBack, resultJson.get("data"));
                    }else{
                        sendonFailureMessage(callBack, resultJson.getString("msg"));
                    }
                    if (response.body() != null) {
                        response.body().close();
                    }
                } else {
                    sendonErrorMessage(callBack, response.code());
                }
            }
        });
    }

    /**
     * 成功信息
     *
     * @param callBack
     * @param object
     */
    private void sendonSuccessMessage(final BaseCallBack callBack, final Object object) {
        mHnadler.post(new Runnable() {
            @Override
            public void run() {
                callBack.onSuccess(object);
            }
        });
    }

    /**
     * 失败信息
     *
     * @param callBack
     * @param msg
     */
    private void sendonFailureMessage(final BaseCallBack callBack,String msg) {
        mHnadler.post(new Runnable() {
            @Override
            public void run() {
                callBack.onFailure(msg);
            }
        });
    }

    /**
     * 错误信息
     *
     * @param callBack
     * @param code
     */
    private void sendonErrorMessage(final BaseCallBack callBack, final int code) {
        mHnadler.post(new Runnable() {
            @Override
            public void run() {
                callBack.onError(code);
            }
        });
    }
}